package com;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author sad
 */
public class Utils {

    public static void showImageDebug(BufferedImage image) {
        final BufferedImage copy = image.getSubimage(0, 0, image.getWidth(), image.getHeight());
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame();
                JPanel panel = new JPanel() {

                    @Override
                    public void paint(Graphics g) {
                        super.paint(g);
                        g.drawImage(copy, 0, 0, getWidth(), getHeight(), this);
                    }
                };
                float factor = (float)copy.getHeight()/copy.getWidth() ;
                panel.setPreferredSize(new Dimension(200, (int) (200 * factor)));
                frame.getContentPane().add(panel);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
                frame.requestFocusInWindow();
            }
        });
    }

    public static void showError(String message) {
        JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE);
    }

    public static int unsignedByte(byte value) {
        return value & 0xff;
    }

    public static int lhTo16Bit(byte l, byte h) {
        return (unsignedByte(h) << 8) | unsignedByte(l);
    }

    public static int toInt(byte[] bytes, int offset) {
        int ret = 0;
        for (int i = 0; i < 4 && i + offset < bytes.length; i++) {
            ret <<= 8;
            ret |= (int) bytes[i] & 0xFF;
        }
        return ret;
    }

    public static void copyBufferToImage(byte[] array, BufferedImage image, int offset) {
        int[] pixel = new int[3];
        int sample = offset;
        WritableRaster raster = image.getRaster();
        for (int j = 0; j < image.getHeight(); j++) {
            for (int i = 0; i < image.getWidth(); i++, sample += 3) {
                pixel[0] = array[sample + 0];
                pixel[1] = array[sample + 1];
                pixel[2] = array[sample + 2];
                raster.setPixel(i, j, pixel);
            }
        }
    }

    public static void copyImageToBuffer(BufferedImage image, byte[] array) {
        int[] pixel = new int[3];
        int index = 0;
        WritableRaster raster = image.getRaster();
        for (int j = 0; j < image.getHeight(); j++) {
            for (int i = 0; i < image.getWidth(); i++, index += 3) {
                raster.getPixel(i, j, pixel);
                array[index] = (byte) pixel[0];
                array[index + 1] = (byte) pixel[1];
                array[index + 2] = (byte) pixel[2];
            }
        }
    }

    public static BufferedImage createOffscreenImage() {
        return new BufferedImage(78, 39, BufferedImage.TYPE_3BYTE_BGR);
    }
    
   // private static final Color emptyColor = new Color(0x83, 0xb3, 0xe2);
    private static final Color emptyColor = new Color(0x0, 0x0, 0x0);
    
    public static void clearImage(BufferedImage image) {
        Graphics g=image.getGraphics();
        g.setColor(emptyColor);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.dispose();
    }
    
    public static void fillImageWithBlack(BufferedImage image) {
        Graphics g=image.getGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, image.getWidth(), image.getHeight());
        g.dispose();
    }
}
